package com.hs.app.helper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hs.app.model.CdcBlfy;
import com.hs.app.model.CdcBlfybd;
import com.hs.app.model.CdcFjZyZfYlzy;
import com.hs.app.model.CdcHerb;
import com.hs.app.model.CdcJsff;
import com.hs.app.model.CdcLhyy;
import com.hs.app.model.CdcLincdz;
import com.hs.app.model.CdcLinchuang;
import com.hs.app.model.CdcLxzb;
import com.hs.app.model.CdcPrehreb;
import com.hs.app.model.CdcSyqk;
import com.hs.app.model.CdcZfBwCf;

/**
 * DWR返回值处理
 * @author Abdi
 */
public class DwrResponseHelper {
	/**
	 * 保存更新响应信息
	 * @return
	 */
	public static String getSaveUpdateResponseMsg(Object id){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("msg", "保存成功");
		if (map == null) {
            return null;
        }
        return JSONObject.fromObject(map).toString();
	}
	
	/**
	 * 保存更新响应信息
	 * @param object
	 * @param formId
	 * @return
	 */
	public static String getSaveUpdateResponseMsg(Object object,String formId,int operator){
		JSONObject response = new JSONObject();
		JSONObject bean = JSONObject.fromObject(object);
		
		JSONArray array = new JSONArray();
		array.add(bean);
		response.put("beans", array);
		
		response.put("formid", formId);
		response.put("operator", operator);
		if(1 == operator){
			response.put("msg", "保存成功!");
		}else if(2 == operator){
			response.put("msg", "修改成功!");
		}else{
			response.put("msg", "操作失败!");
		}
		return response.toString();
	}
	
	/**
	 * 获取一条数据响应信息
	 * @param object
	 * @param formId
	 * @return
	 */
	public static String getOneObjResponse(Object object,String formId){
		JSONObject response = new JSONObject();
		JSONObject bean = JSONObject.fromObject(object);
		
		JSONArray array = new JSONArray();
		array.add(bean);
		response.put("beans", array);
		response.put("formid", formId);
		return response.toString();
	}
	
	/**
	 * 返回一组数据响应信息
	 * @param list
	 * @param formId
	 * @return
	 */
	public static String getListObjResponse(List<Object> list,String formId){
		JSONObject response = new JSONObject();
		JSONArray array = new JSONArray();
		if(null != list){
			for(Object o : list){
				JSONObject bean = JSONObject.fromObject(o);
				array.add(bean);
			}
		}
		
		response.put("beans", array);
		response.put("formid", formId);
		return response.toString();
	}
	
	/**
	 * 删除响应信息
	 * @param object
	 * @param formId
	 * @return
	 */
	public static String getDelResponseMsg(String formId){
		JSONObject response = new JSONObject();
		response.put("formid", formId);
		response.put("msg", "删除成功!");
		return response.toString();
	}
	
	/**
	 * 获取类对象
	 * @param dataType
	 * @return
	 */
	public static Class<?> getObjectClazz(String formId){
		return clazzMap.get(formId);
	}
	
	/**
	 * 获取附表键关系分布
	 * @param formId
	 * @return
	 */
	public static String[] getKeyArray(String formId){
		return keyMap.get(formId);
	}
	
	/**
	 * 需级联删除的主附表关系 
	 */
	private static Map<String,String[]> tableMap = new HashMap<String, String[]>();
	static{
		tableMap.put("syqk", new String[]{"syqk","jsff","ylzy","sylxzb"});
		tableMap.put("zycy", new String[]{"zycy","zyzz"});
		tableMap.put("linchuang", new String[]{"linchuang","lhyy","blfyxx","lxzb","lcdz"});
	}

	/**
	 * 附表键映射
	 */
	private static Map<String,String[]> keyMap = new HashMap<String, String[]>();
	static{
		keyMap.put("jsff", new String[]{"pid","syid"});
		keyMap.put("ylzy", new String[]{"pid","syid"});
		keyMap.put("sylxzb", new String[]{"pid","rid","rtype"});
		
		keyMap.put("zyzz", new String[]{"pid","zycyid"});
		keyMap.put("zblfybd", new String[]{"pid","rid","rtype"});
		keyMap.put("zhxcf", new String[]{"pid","rid","rtype"});
		
		keyMap.put("lcdz", new String[]{"pid","lcid"});
		keyMap.put("blfyxx", new String[]{"pid","lcid"});
		keyMap.put("lxzb", new String[]{"pid","rid","rtype"});
		keyMap.put("lhyy", new String[]{"pid","lcid"});
	}
	
	/**
	 * 类型与类对照表 
	 */
	private static Map<String,Class<?>> clazzMap = new HashMap<String, Class<?>>();
	static{
		clazzMap.put("syqk", CdcSyqk.class); // 实验情况信息
		clazzMap.put("jsff", CdcJsff.class); // 技术方法信息
		clazzMap.put("ylzy", CdcFjZyZfYlzy.class); // 方剂-中药-组分药理作用信息
		clazzMap.put("sylxzb", CdcLxzb.class); // 疗效指标信息  rtype = 1

		clazzMap.put("zycy", CdcPrehreb.class); // 中药成药信息
		clazzMap.put("zyzz", CdcHerb.class); // 中药组成信息
		clazzMap.put("zblfybd", CdcBlfybd.class);
		clazzMap.put("zhxcf", CdcZfBwCf.class);
		
		clazzMap.put("blfybd", CdcBlfybd.class); // 不良反应报道

		clazzMap.put("linchuang", CdcLinchuang.class); // 临床观察信息
		clazzMap.put("lhyy", CdcLhyy.class); // 联合用药信息
		clazzMap.put("blfyxx", CdcBlfy.class); // 不良反应信息
		clazzMap.put("lxzb", CdcLxzb.class); // 疗效指标信息 rtype = 2
		clazzMap.put("lcdz", CdcLincdz.class); // 临床对照信息 
		
		clazzMap.put("hxcf", CdcZfBwCf.class); // 组分-部位-（活性）成分
	}
}
